package com.ce.common.base;


import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.Toast;

import com.ce.common.AppComponent;
import com.ce.common.GlobalAppComponent;
import com.ce.common.model.DataManager;
import com.ce.common.receiver.NetWorkChangeBroadcastReceiver;
import com.ce.common.util.DialogUtil;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import butterknife.Unbinder;

/**
 * Created by admin on 2017/3/12.
 */

public abstract class BaseActivity extends RxAppCompatActivity {
    protected DataManager mDataManager;
    protected Context mContext;
    protected Dialog loadingDialog;
    protected Unbinder unbinder;
    private NetWorkChangeBroadcastReceiver receiver;

    private BroadcastReceiver mBaseReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            int code = getResultCode();
            doBroadcastReceive(context, intent, code);
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDataManager = getAppComponent().getDataManager();
        mContext = getAppComponent().getContext();
        registerNetChangeReceiver();
        this.getApplication();

        IntentFilter filter = new IntentFilter();
        String[] actions = getBroadcastActions();
        if (actions != null) {
            for (int i = 0; i < actions.length; i++) {
                filter.addAction(actions[i]);
            }
        }
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        registerReceiver(mBaseReceiver, filter);
    }

    protected void doBroadcastReceive(Context context, Intent intent, int code) {
    }

    private void registerNetChangeReceiver() {
        receiver = new NetWorkChangeBroadcastReceiver(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(receiver, intentFilter);
    }

    protected String[] getBroadcastActions() {
        return null;
    }

    protected AppComponent getAppComponent() {
        return GlobalAppComponent.getAppComponent();
    }

    protected void addFragment(int containerViewId, Fragment fragment, String tag) {
        final FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();

        fragmentTransaction.add(containerViewId, fragment, tag);
        fragmentTransaction.commit();
    }

    protected void showShortToast(String message) {
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
    }

    protected void showLongToast(String message) {
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
    }

    protected void showProgressDialog() {
        this.showProgressDialog("小塘拼命加载中...", null);
    }

    protected void showProgressDialog(String msg) {
        this.showProgressDialog(msg, null);
    }

    protected void showProgressDialog(DialogInterface.OnCancelListener listener) {
        this.showProgressDialog(null, listener);
    }

    protected void showProgressDialog(String msg, DialogInterface.OnCancelListener listener) {
        if (loadingDialog == null) {
            loadingDialog = DialogUtil.createLoadingDialog(this, msg, listener);
        } else if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }

    }

    protected void hiddenProgressDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }

        if (unbinder != null) {
            unbinder.unbind();
        }

        if (null != receiver) {
            receiver.onDestroy();
            unregisterReceiver(receiver);
            receiver = null;
        }
        if (null != mBaseReceiver) {
            unregisterReceiver(mBaseReceiver);
            receiver = null;
        }
    }


}
